package com.openbravo.pos.admin;

import com.openbravo.basic.BasicException;
import com.openbravo.data.gui.ComboBoxValModel;
import com.openbravo.data.gui.JImageEditor;
import com.openbravo.data.loader.ImageUtils;
import com.openbravo.data.user.DirtyManager;
import com.openbravo.data.user.EditorRecord;
import com.openbravo.format.Formats;
import com.openbravo.pos.forms.AppLocal;
import com.openbravo.pos.util.Base64Encoder;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Component;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.UUID;

import javax.swing.ButtonGroup;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.GroupLayout.ParallelGroup;
import javax.swing.GroupLayout.SequentialGroup;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.text.Document;

public class ResourcesView extends JPanel
  implements EditorRecord
{
  private Object m_oId;
  private ComboBoxValModel m_ResourceModel;
  private JLabel jLabel2;
  private JPanel jPanel1;
  private JPanel jPanel3;
  private JScrollPane jScrollPane1;
  private JPanel m_jContainer;
  private ButtonGroup m_jGroupType;
  private JImageEditor m_jImage;
  private JTextField m_jName;
  private JTextArea m_jText;
  private JComboBox m_jType;

  public ResourcesView(DirtyManager paramDirtyManager)
  {
    initComponents();
    this.m_ResourceModel = new ComboBoxValModel();
    this.m_ResourceModel.add(ResourceType.TEXT);
    this.m_ResourceModel.add(ResourceType.IMAGE);
    this.m_ResourceModel.add(ResourceType.BINARY);
    this.m_jType.setModel(this.m_ResourceModel);
    this.m_jName.getDocument().addDocumentListener(paramDirtyManager);
    this.m_jType.addActionListener(paramDirtyManager);
    this.m_jText.getDocument().addDocumentListener(paramDirtyManager);
    this.m_jImage.addPropertyChangeListener("image", paramDirtyManager);
    writeValueEOF();
  }

  public void writeValueEOF()
  {
    this.m_oId = null;
    this.m_jName.setText(null);
    this.m_ResourceModel.setSelectedItem(null);
    this.m_jText.setText(null);
    this.m_jImage.setImage(null);
    this.m_jName.setEnabled(false);
    this.m_jType.setEnabled(false);
    this.m_jText.setEnabled(false);
    this.m_jImage.setEnabled(false);
  }

  public void writeValueInsert()
  {
    this.m_oId = null;
    this.m_jName.setText(null);
    this.m_ResourceModel.setSelectedItem(ResourceType.TEXT);
    this.m_jText.setText(null);
    this.m_jImage.setImage(null);
    this.m_jName.setEnabled(true);
    this.m_jType.setEnabled(true);
    this.m_jText.setEnabled(true);
    this.m_jImage.setEnabled(true);
  }

  public void writeValueDelete(Object paramObject)
  {
    Object[] arrayOfObject = (Object[])(Object[])paramObject;
    this.m_oId = arrayOfObject[0];
    this.m_jName.setText((String)arrayOfObject[1]);
    this.m_ResourceModel.setSelectedKey(arrayOfObject[2]);
    ResourceType localResourceType = (ResourceType)this.m_ResourceModel.getSelectedItem();
    if (localResourceType == ResourceType.TEXT)
    {
      this.m_jText.setText(Formats.BYTEA.formatValue(arrayOfObject[3]));
      this.m_jText.setCaretPosition(0);
      this.m_jImage.setImage(null);
    }
    else if (localResourceType == ResourceType.IMAGE)
    {
      this.m_jText.setText(null);
      this.m_jImage.setImage(ImageUtils.readImage((byte[])(byte[])arrayOfObject[3]));
    }
    else if (localResourceType == ResourceType.BINARY)
    {
      this.m_jText.setText(arrayOfObject[3] == null ? null : Base64Encoder.encodeChunked((byte[])(byte[])arrayOfObject[3]));
      this.m_jText.setCaretPosition(0);
      this.m_jImage.setImage(null);
    }
    else
    {
      this.m_jText.setText(null);
      this.m_jImage.setImage(null);
    }
    this.m_jName.setEnabled(false);
    this.m_jType.setEnabled(false);
    this.m_jText.setEnabled(false);
    this.m_jImage.setEnabled(false);
  }

  public void writeValueEdit(Object paramObject)
  {
    Object[] arrayOfObject = (Object[])(Object[])paramObject;
    this.m_oId = arrayOfObject[0];
    this.m_jName.setText((String)arrayOfObject[1]);
    this.m_ResourceModel.setSelectedKey(arrayOfObject[2]);
    ResourceType localResourceType = (ResourceType)this.m_ResourceModel.getSelectedItem();
    if (localResourceType == ResourceType.TEXT)
    {
      this.m_jText.setText(Formats.BYTEA.formatValue(arrayOfObject[3]));
      this.m_jText.setCaretPosition(0);
      this.m_jImage.setImage(null);
    }
    else if (localResourceType == ResourceType.IMAGE)
    {
      this.m_jText.setText(null);
      this.m_jImage.setImage(ImageUtils.readImage((byte[])(byte[])arrayOfObject[3]));
    }
    else if (localResourceType == ResourceType.BINARY)
    {
      this.m_jText.setText(arrayOfObject[2] == null ? null : Base64Encoder.encodeChunked((byte[])(byte[])arrayOfObject[3]));
      this.m_jText.setCaretPosition(0);
      this.m_jImage.setImage(null);
    }
    else
    {
      this.m_jText.setText(null);
      this.m_jImage.setImage(null);
    }
    this.m_jName.setEnabled(true);
    this.m_jType.setEnabled(true);
    this.m_jText.setEnabled(true);
    this.m_jImage.setEnabled(true);
  }

  public Object createValue()
    throws BasicException
  {
    Object[] arrayOfObject = new Object[4];
    arrayOfObject[0] = (this.m_oId == null ? UUID.randomUUID().toString() : this.m_oId);
    arrayOfObject[1] = this.m_jName.getText();
    ResourceType localResourceType = (ResourceType)this.m_ResourceModel.getSelectedItem();
    arrayOfObject[2] = localResourceType.getKey();
    if (localResourceType == ResourceType.TEXT)
      arrayOfObject[3] = Formats.BYTEA.parseValue(this.m_jText.getText());
    else if (localResourceType == ResourceType.IMAGE)
      arrayOfObject[3] = ImageUtils.writeImage(this.m_jImage.getImage());
    else if (localResourceType == ResourceType.BINARY)
      arrayOfObject[3] = Base64Encoder.decode(this.m_jText.getText());
    else
      arrayOfObject[3] = null;
    return arrayOfObject;
  }

  public Component getComponent()
  {
    return this;
  }

  public void refresh()
  {
  }

  private void showView(String paramString)
  {
    CardLayout localCardLayout = (CardLayout)(CardLayout)this.m_jContainer.getLayout();
    localCardLayout.show(this.m_jContainer, paramString);
  }

  private void initComponents()
  {
    this.m_jGroupType = new ButtonGroup();
    this.jPanel3 = new JPanel();
    this.m_jContainer = new JPanel();
    this.jScrollPane1 = new JScrollPane();
    this.m_jText = new JTextArea();
    this.jPanel1 = new JPanel();
    this.m_jImage = new JImageEditor();
    this.jLabel2 = new JLabel();
    this.m_jName = new JTextField();
    this.m_jType = new JComboBox();
    this.jPanel3.setLayout(new BorderLayout());
    this.m_jContainer.setLayout(new CardLayout());
    this.m_jText.setFont(new Font("DialogInput", 0, 12));
    this.jScrollPane1.setViewportView(this.m_jText);
    this.m_jContainer.add(this.jScrollPane1, "text");
    this.m_jContainer.add(this.jPanel1, "null");
    this.m_jContainer.add(this.m_jImage, "image");
    this.jPanel3.add(this.m_jContainer, "Center");
    this.jLabel2.setText(AppLocal.getIntString("label.resname"));
    this.m_jType.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent paramActionEvent)
      {
        ResourcesView.this.m_jTypeActionPerformed(paramActionEvent);
      }
    });
    GroupLayout localGroupLayout = new GroupLayout(this);
    setLayout(localGroupLayout);
    localGroupLayout.setHorizontalGroup(localGroupLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(localGroupLayout.createSequentialGroup().addContainerGap().addGroup(localGroupLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(this.jPanel3, -1, 549, 32767).addGroup(localGroupLayout.createSequentialGroup().addComponent(this.jLabel2, -2, 90, -2).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(this.m_jName, -2, 180, -2).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(this.m_jType, -2, 90, -2))).addContainerGap()));
    localGroupLayout.setVerticalGroup(localGroupLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(localGroupLayout.createSequentialGroup().addContainerGap().addGroup(localGroupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(this.jLabel2).addComponent(this.m_jName, -2, -1, -2).addComponent(this.m_jType, -2, 20, -2)).addGap(18, 18, 18).addComponent(this.jPanel3, -1, 327, 32767).addContainerGap()));
  }

  private void m_jTypeActionPerformed(ActionEvent paramActionEvent)
  {
    ResourceType localResourceType = (ResourceType)this.m_ResourceModel.getSelectedItem();
    if (localResourceType == ResourceType.TEXT)
      showView("text");
    else if (localResourceType == ResourceType.IMAGE)
      showView("image");
    else if (localResourceType == ResourceType.BINARY)
      showView("text");
    else
      showView("null");
  }
}

/* Location:           D:\PowerFulPOS-2.30.2\openbravopos.jar
 * Qualified Name:     com.openbravo.pos.admin.ResourcesView
 * JD-Core Version:    0.6.0
 */